package com.wzd.learn.springcloud.service.b;

import java.lang.reflect.Method;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.wzd.learn.springcloud.service.b.annotation.CuzFeignHystrix;

import feign.Feign;
import feign.Target;
import feign.hystrix.HystrixFeign;
import feign.hystrix.SetterFactory;

@Configuration
public class WebConfig {
	/**
	 * 自定义feign Hystrix的配置
	 * 
	 * @return
	 */
	@Bean
	public Feign.Builder feignHystrixBuilder() {
		return HystrixFeign.builder().setterFactory(new SetterFactory() {
			@Override
			public HystrixCommand.Setter create(Target<?> target, Method method) {

				String groupKey = target.name();
				String commandKey = Feign.configKey(target.type(), method);
				CuzFeignHystrix annotation = method
						.getDeclaredAnnotation(CuzFeignHystrix.class);
				if (annotation != null) {
					commandKey = annotation.commandKey();
				}
				return HystrixCommand.Setter.withGroupKey(
						HystrixCommandGroupKey.Factory.asKey(groupKey))
						.andCommandKey(
								HystrixCommandKey.Factory.asKey(commandKey));
			}
		});
	}
}
